package com.diankangwo.factory.controller;

import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.factory.annotation.LoginUser2;
import com.diankangwo.factory.entity.TdNews;
import com.diankangwo.factory.service.TdNewsService;
import com.diankangwo.sys.entity.SysUserEntity;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 新闻Controller
 */
@RestController
@RequestMapping("/fac/news")
public class TdNewsController {

    @Autowired
    private TdNewsService tdNewsService;

    /**
     * 获取新闻列表
     */
    @ApiOperation(value = "获取新闻列表", notes = "")
    @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>")
    @RequestMapping("/list")
    @RequiresPermissions("fac:news:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TdNews> tdRefundMonies = tdNewsService.queryList(query);
        //为DTO赋值，并查询出用户信息参数
        int total = tdNewsService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tdRefundMonies, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 添加/修改新闻
     */
    @ApiOperation(value = "获取新闻列表", notes = "")
    @ApiImplicitParam(name = "tdNews", value = "新闻", required = true, dataType = "TdNews")
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions("fac:news:saveOrUpdate")
    public R saveOrUpdate(@LoginUser2 SysUserEntity sysUserEntity, TdNews tdNews) {
        if (tdNews.getId() != null) {
            TdNews t = tdNewsService.selectByPrimaryKey(tdNews.getId());
            tdNews.setUpdateuser(sysUserEntity.getUsername());
            tdNews.setCreatedate(t.getCreatedate());
            tdNews.setCreateuser(t.getCreateuser());
            tdNews.setUpdatedate(new Date());
            tdNewsService.updateByPrimaryKey(tdNews);
        } else {
            tdNews.setUpdateuser(sysUserEntity.getUsername());
            tdNews.setCreatedate(new Date());
            tdNewsService.insert(tdNews);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @RequestMapping("/delete")
    @RequiresPermissions("fac:news:delete")
    public R delete(int id) {
        tdNewsService.deleteByPrimaryKey(id);
        return R.ok();
    }

    /**
     * 按ID 查询详情
     */
    @ApiOperation(value = "删除", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @RequestMapping("/findById")
    @RequiresPermissions("fac:news:findById")
    public R findById(int id) {
        return R.ok().put("tdNews", tdNewsService.selectByPrimaryKey(id));
    }
}
